package com.east.sng.service.impl;

import com.east.sng.dao.MenuDao;
import com.east.sng.entity.param.MenuParam;
import com.east.sng.entity.result.MenuFind;
import com.east.sng.entity.result.MenuInfo;
import com.east.sng.entity.result.MenuTree;
import com.east.sng.service.MenuService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @author east
 * @date: 2021/1/5
 * @Description:
 **/
@Service
public class MenuServiceImpl implements MenuService {
    @Autowired
    private MenuDao menuDao;

    @Override
    public List<MenuInfo> findMenus(Map<String, Object> params) {
        return this.menuDao.selectMenus(params);
    }

    @Override
    public List<Map<String,Object>> findParentMenus() {
        return this.menuDao.selectParentMenus();
    }

    @Override
    public MenuFind findMenu(Integer menuId) {
        return this.menuDao.selectMenu(menuId);
    }

    @Override
    public void addMenu(MenuParam menu) {
        this.menuDao.insertMenu(menu);
    }

    @Override
    public void editMenu(MenuParam menu) {
        this.menuDao.updateMenu(menu);
    }

    @Override
    public boolean removeMenu(Integer menuId) {
        List<Integer> ids = new ArrayList<>();
        List<MenuTree> list = this.menuDao.selectMenuByPid(menuId);
        list.forEach((item)->{
            if (item!=null) {
                ids.add(item.getMenuId());
            }
        });
        ids.add(menuId);
        System.out.println("ids = " + ids);
        return this.menuDao.batchDeleteOrRecoverMenus(ids,1)>0;
    }

    @Override
    public boolean recoverMenu(Integer menuId) {
        List<Integer> ids = new ArrayList<>();
        List<MenuTree> list = this.menuDao.selectMenuByPid(menuId);
        list.forEach((item)->{
            if (item!=null) {
                ids.add(item.getMenuId());
            }
        });
        ids.add(menuId);
        System.out.println("ids = " + ids);
        return this.menuDao.batchDeleteOrRecoverMenus(ids,0)>0;
    }

    @Override
    public boolean batchRemoveMenus(List<Integer> menuIds) {
        List<Integer> ids = new ArrayList<>();
        for (Integer pid : menuIds) {
            List<MenuTree> menu = this.menuDao.selectMenuByPid(pid);
            if (menu!=null) {
                menu.forEach((item)->{
                    ids.add(item.getMenuId());
                });
                ids.add(pid);
            } else {
                ids.add(pid);
            }
        }
        System.out.println("ids = " + ids);
        return this.menuDao.batchDeleteOrRecoverMenus(menuIds,1)>0;
    }

    @Override
    public boolean batchRecoverMenus(List<Integer> menuIds) {
        List<Integer> ids = new ArrayList<>();
        for (Integer pid : menuIds) {
            List<MenuTree> menu = this.menuDao.selectMenuByPid(pid);
            if (menu!=null) {
                for (MenuTree m : menu) {
                    ids.add(m.getMenuId());
                }
                ids.add(pid);
            } else {
                ids.add(pid);
            }
        }
        return this.menuDao.batchDeleteOrRecoverMenus(menuIds,0)>0;
    }

    @Override
    public List<MenuTree> findMenuByPid(Integer pid) {
        return this.menuDao.selectMenuByPid(pid);
    }

    @Override
    public List<Integer> findAssignMenuIdsByRoleId(Integer roleId) {
        return this.menuDao.selectAssignMenuIdsByRoleId(roleId);
    }

    @Override
    public List<MenuTree> findMenuByRoleId(Integer roleId) {
        return this.menuDao.selectMenuByRoleId(roleId);
    }
}
